package com.utils.parsetxt.property;

import com.utils.web.jdbc.MySQLConnect;

import java.io.*;
import java.util.*;

public class txtUtil {
	
	private static final String writeUserPath="D:\\LocoDown\\fabu\\入库用户.txt";
	
	@SuppressWarnings("unused")
	public static void getUserName(String filePath) {
		BufferedReader reader;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
			String line = "";
			String[] jpgs = null;
			String[] pngs = null;
			Set<String> nameSet = new HashSet<String>();
			Set<String> pathSet = new HashSet<String>();
			
			Map<String, String> map = new HashMap<String, String>();
			
			while ((line = reader.readLine()) != null) {
				String name = "";
				String path = "";
				if(line.indexOf("jpg")!=-1) {//包含jpg
					jpgs = line.split("jpg");
					name = jpgs[1].trim();
					path = jpgs[0].trim();
				}else if(line.indexOf("png")!=-1){//包含png
					pngs = line.split("png");
//					name = pngs[1];
//				    path = pngs[0];
				}else {
					System.out.println("这是第一行："+line.trim());
					name = line.trim();
				}
				if(!"".equals(name)&&name.length()<9) {
					nameSet.add(replaceStr(name));
				}
				if(!"".equals(path)&&path.indexOf("nopic")==-1&&path.indexOf("遮光罩")==-1) {
					pathSet.add(path+"jpg");
				}
			}
			System.out.println(nameSet.size());
			System.out.println(pathSet.size());

			
			List<String> nameList = new ArrayList<String>(nameSet);
			List<String> pathList = new ArrayList<String>(pathSet);

			for (int i = 3; i < 103; i++) {
				map.put(nameList.get(i),"http://eovlo.cn/joke/avatar/"+ 
						pathList.get(i+2)
						.substring(pathList.get(i+2).lastIndexOf("/")+1,pathList.get(i+2).length() ));
			}
			System.out.println(map.size());
			for (String string : map.keySet()) {
				System.out.println("name="+string+":"+"path="+map.get(string));
			}
	
			//保存入库用户
			writeUser(map);
			//插入数据库
			MySQLConnect.insertUser(map);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
	}
	
	/**
	 * 将入库的用户做个备份
	 * @param map
	 */
	public static void writeUser(Map<String,String> map) {
		BufferedWriter out = null;
		try {
			/* 写入Txt文件 */  
			File writename = new File(writeUserPath); // 相对路径，如果没有则要建立一个新的output。txt文件  
			writename.createNewFile();  // 创建新文件  
			out = new BufferedWriter(new FileWriter(writename));
			for (String str : map.keySet()) {
				out.write(str+"\r\n");// \r\n即为换行  
				out.write(map.get(str)+"\r\n");// \r\n即为换行
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}  finally {
			try {
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} // 把缓存区内容压入文件  
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			} // 最后记得关闭文件  
		}
	}
	
	/**
	 * 替换部分字符
	 * @param str
	 * @return
	 */
	public static String replaceStr(String str) {
		str = str.replace("王八", "乌龟").replace("上", "下").replace("胖", "瘦").replace("香", "香香").
		replace("大", "小").replace("小", "大").replace("东", "西").replace("下", "上").
		replace("天", "地").replace("月亮", "太阳").replace("之", "的").replace("人", "").
		replace("？", "").replace("！", "").replace("。", "").replace("，", "").replace("的", "之")
		.replace("与", "和");
		return str;
	}
	
	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		String path = "D:\\\\LocoDown\\\\fabu\\\\2018-03-11 21-16-47.txt";
		getUserName(path);
	}

}
